package com.fingard.dsp.bank.directbank.lsbank01;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;

/**
 * Created by F on 2018/4/9.
 */
public class LSBANK01Transaction extends LSBANK01Base implements ITransaction {

    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        RetAtsTrans retAtsTrans = new RetAtsTrans(p_req7516.reqHeader);
        String respCode = "";
        String respInfo = "";
        String opName ="queryAccHisDetail";
        retAtsTrans.writeStart();
       try{
            Map<String,String> reqParam = new HashMap<String, String>();
            reqParam.put("accountNo",p_req7516.actNum);
            reqParam.put("beginDate",p_req7516.dateFrom);
            reqParam.put("endDate",p_req7516.dateTo);
            String[] bankResult = sendToBank(reqParam,p_req7516.reqHeader,opName);
                if(bankResult[0].length()==0){
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpSbMsg);
                    if(tmpBkReader!=null){
                        String hostReturnCode = getHostReturnCode(tmpBkReader);
                        if(isSuccess(hostReturnCode)){
                            if(Integer.valueOf(tmpBkReader.getFirstText("/BankData/opRep/turnPageTotalNum"))==0){
                                respCode = "0";
                                respInfo = "交易为空";
                            }else{
                                respCode = "0";
                                respInfo = "success";
                                while(tmpBkReader.read("/BankData/opRep/RepParam/tranDetail/row")){
                                    XElement readXElement = tmpBkReader.readedXElement;
                                    RetAtsTransDetail detail = new RetAtsTransDetail();
                                    String transferDate = readXElement.getSingleText("transferDate").trim();
                                    String transferTime = readXElement.getSingleText("transferTime").trim();
                                    //transferDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
                                    //transferDate = dicTime(transferDate);
                                    detail.tranDate = DateHelper.getFormat12Date(transferDate);
                                    detail.valueDate = detail.tranDate;
                                    detail.tranTime = detail.tranDate+" "+DateHelper.getFormat8Time(transferTime);
                                    detail.comment = readXElement.getSingleTextNotNull("rem").trim();
                                    detail.usage =readXElement.getSingleTextNotNull("summary").trim();
                                    detail.askNumber = readXElement.getSingleTextNotNull("transferFlowNo").trim();
                                    detail.payCode =readXElement.getSingleTextNotNull("vouchersNo").trim();
                                    detail.recAccNum =readXElement.getSingleTextNotNull("account").trim();
                                    detail.recAccName =readXElement.getSingleTextNotNull("accountName").trim();
                                    detail.recAccBank =readXElement.getSingleTextNotNull("accountBankNo").trim();
                                    detail.cur = convertCurToATS(readXElement.getSingleTextNotNull("currencyType").trim());
                                    String payAmount = readXElement.getSingleTextNotNull("payAmount").trim();
                                    if(!payAmount.replace("0","").replace(".","").equals("")){
                                        detail.tranAmt = payAmount.replace("A","").replace("a","").replaceFirst("^0*","");
                                        if(payAmount.startsWith("A")||payAmount.startsWith("a")){
                                            detail.cdFlag = "2";
                                        }else{
                                            detail.cdFlag = "1";
                                        }
                                    }else {
                                        String recAmount = readXElement.getSingleTextNotNull("recAmount");
                                        detail.tranAmt = recAmount.replace("A","").replace("a","").replaceFirst("^0*","");
                                        if(recAmount.startsWith("A")||recAmount.startsWith("a")){
                                            detail.cdFlag="1";
                                        }else{
                                            detail.cdFlag="2";
                                        }
                                    }
                                    detail.accBal = readXElement.getSingleTextNotNull("balance").replaceFirst("^0*","");
                                    if(detail.accBal.startsWith(".")){
                                        detail.accBal=0+detail.accBal;
                                    }
                                    if(detail.tranAmt.startsWith(".")){
                                        detail.tranAmt=0+detail.tranAmt;
                                    }
                                    retAtsTrans.writeRetDetail(detail);
                                }
                            }
                        }else {
                            respCode = "-1";
                            respInfo = getHostErrorMessage(tmpBkReader);
                        }
                    }else{
                        respCode="-1";
                        respInfo = tmpSbMsg.toString();
                    }
                }else{
                    respCode ="-1";
                    respInfo=bankResult[1];
                }

            }catch (Exception e){
                respCode = "-1";
                respInfo = e.getMessage();
               e.printStackTrace();
        }
        if(respCode.equals("-1")){
            retAtsTrans.writeRespInfo(respInfo);
            retAtsTrans.writeRespCode(respCode);
        }else if(retAtsTrans.getWrittenRdCount()==0){
            retAtsTrans.writeRespInfo(respInfo);
            retAtsTrans.writeRespCode(respCode);
        }
        retAtsTrans.writeEnd();
        return retAtsTrans;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        Req7516HisTrans p_req7516 = new Req7516HisTrans(p_req7515.reqHeader);
        p_req7516.actArea = p_req7515.actArea;
        p_req7516.actCur = p_req7515.actCur;
        p_req7516.actNum = p_req7515.actNum;
        String date = Format.Date8Format.format(new Date());
        p_req7516.dateFrom = date;
        p_req7516.dateTo = date;
        return d_7516_HisTrans(p_req7516);
    }

}
